package com.milk.controller.user;


import com.milk.dto.ShoppingCarDTO;
import com.milk.dto.ShoppingCarDeleteDTO;
import com.milk.entity.ShoppingCart;
import com.milk.result.Result;

import com.milk.service.IShoppingCarService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCar")
@ApiModel(value = "购物车接口")
@Slf4j
public class ShoppingCarController {
    @Autowired
    public IShoppingCarService shoppingCarService;


    /**
     * 添加购物车
     * @param dto
     * @return
     */
    @PostMapping("/add")
    @ApiModelProperty("添加购物车")
    public Result add(@RequestBody ShoppingCarDTO dto){
        log.info("dto = {}",dto);

        shoppingCarService.add(dto);
        return Result.success();

    }

    /**
     * 减少一个购物车奶茶数量根据购物车的id
     * @return
     */
    @DeleteMapping("/deleteOne/{id}")
    @ApiModelProperty("减少一个购物车奶茶数量")
    public Result deleteOne(@PathVariable Long id){
        log.info("id = {}",id);
        shoppingCarService.deleteOne(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    @DeleteMapping
    @ApiModelProperty("批量删除")
    public Result deleteById(@RequestBody ShoppingCarDeleteDTO dto){
        shoppingCarService.deleteById(dto);
        return Result.success();
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping
    @ApiModelProperty("查询购物车")
    public Result<List<ShoppingCart>> selectAll(){
        List<ShoppingCart> list = shoppingCarService.selectAll();
        return Result.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/all")
    public Result deleteAll(){
        shoppingCarService.deleteAll();
        return Result.success();
    }
}
